package br.ufpb.di.ppgi.persistencia.query;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;

import br.ufpb.di.ppgi.persistencia.domainstore.DomainStore;

public abstract class AbstractJPAHibernateQuery extends AbstractJPAQuery {
	protected final Session session;
	
	public AbstractJPAHibernateQuery(DomainStore domainStore) {
		super(domainStore);
		session = (Session) getManager().getDelegate();
	}
	
	protected Session getSession() {
		return session;
	}
	
	protected Criteria createCriteria(Class<?> target) {
		return getSession().createCriteria(target);
	}
	
	@SuppressWarnings("unchecked")
	protected <T> T unique(Criteria criteria) {
		return (T) criteria.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	protected <T> List<T> list(Criteria criteria) {
		return criteria.list();
	}
	
	protected Integer count(Criteria criteria) {
		return (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
	}
}
